package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.Shop;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.result.PageResult;
import net.qhzw.vip.qiyu.service.ShopService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/***
 * 商铺管理
 */
@RestController
@RequestMapping("sys/shop")
@Api(tags = "商铺管理")
public class SysShopController {

    @Autowired
    private ShopService shopService;

    @ApiOperation(value = "分页查询商铺")
    @GetMapping
    public JsonResult<PageResult<Shop>> findByPage(Page<Shop> page, String shopName) {
        //Long shopId = SecurityUtils.getLoginUser().getShopId();
        PageResult<Shop> pageResult = shopService.findByPage(page, shopName);
        return JsonResult.ok(pageResult);
    }

    @ApiOperation(value = "根据id查询商铺")
    @GetMapping("/{shopId}")
    public JsonResult<Shop> findById(@PathVariable Long shopId) {
        Shop shop = shopService.getById(shopId);
        shop.setAdminPassword("");
        return JsonResult.ok(shop);
    }


    @ApiOperation(value = "添加商铺")
    @PostMapping
    public JsonResult addShop(@RequestBody Shop shop) {
        Long userId = SecurityUtils.getUserId();
        boolean isOK = shopService.addShop(userId, shop);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "修改商铺")
    @PutMapping
    public JsonResult revise(@RequestBody Shop shop) {
        shop.setLastUpdateTime(new Date());
        boolean isOK = shopService.updateById(shop);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改商铺状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody Shop shop) {
        boolean isOK = shopService.setStatus(shop);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "修改shifou展示状态")
    @PutMapping("setStatusShow")
    public JsonResult setIsShow(@RequestBody Shop shop) {
        boolean isOK = shopService.setStatusShow(shop);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改大屏是否展示状态")
    @PutMapping("setisShow/{ids}")
    public JsonResult setisShow(@PathVariable String ids) {
        List<String> uids= Arrays.asList(ids.split(","));
        boolean isOK = shopService.setIsShow(uids);

        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

}
